<template>
  <van-popup
    :show="visible"
    :close-on-click-overlay="false"
    round
    safe-area-inset-bottom
    safe-area-inset-top
    position="bottom"
    class="m-select"
    @open="bindGet"
  >
    <div class="m-select-title">
      <span class="m-title-block">{{ title }}</span>
      <span class="m-select-all" @click="selectAll">
        <van-icon v-if="allStatus" name="checked" color="#1989fa" />
        <van-icon v-else name="circle" />
        <ins>{{ selectAllText }}</ins>
      </span>
    </div>
    <div class="m-select-content" :style="{ height: `${height}px` }">
      <van-checkbox-group ref="checkgroup" v-model="result">
        <van-cell-group>
          <van-cell
            v-for="(item, i) of data"
            :key="i"
            :title="item.label"
            clickable
            @click="toggle(i)"
          >
            <template #right-icon>
              <van-checkbox ref="checkboxes" :name="item.value" checked-color="#1989fa" @click.stop />
            </template>
          </van-cell>
        </van-cell-group>
      </van-checkbox-group>
    </div>
    <!-- 按钮自定义 -->
    <slot name="foot">
      <div class="m-control-btn">
        <van-button
          type="primary"
          size="small"
          round
          class="m-btn-confirm m-btn"
          @click="confirm"
          >确认</van-button
        >
        <van-button
          type="default"
          size="small"
          round
          class="m-btn-cancel m-btn"
          @click="cancel"
          >取消</van-button
        >
      </div>
    </slot>
  </van-popup>
</template>
<script>
import { defineComponent, computed, ref, toRefs } from "vue"
import { deepCopy } from "@/utils"
export default defineComponent({
  name: "MSelect",
  emits: ['update:show', 'update:modelValue'],
  props: {
    value: {
      type: Array,
    },
    visible: {
      type: Boolean,
    },
    title: {
      type: String,
    },
    height: {
      type: Number,
      default: 280,
    },
    data: {
      type: Array,
      default: () => [],
    },
    selectAllText: {
      type: String,
      default: "全选",
    },
  },
  setup(props, { emit }) {
    const { value, data, height, title, visible, selectAllText } = toRefs(props)

    // refs
    const checkgroup = ref(null)
    const checkboxes = ref(null)
    const result = ref([])
    // select all
    const allStatus = computed(() => result.value.length === data.value.length)

    // popup init
    const bindGet = () => {
      if (value.value) result.value = deepCopy(value.value)
    }
    const toggle = (index) => {
      checkboxes.value[index].toggle()
    }
    const selectAll = () => {
      checkgroup.value.toggleAll(!allStatus.value)
    }
    const confirm = () => {
      emit('update:modelValue', result.value)
      cancel()
    }
    const cancel = () => {
      emit('update:show', false)
    }
    return {
      selectAllText,
      allStatus,
      height,
      title,
      data,
      visible,
      result,
      checkgroup,
      checkboxes,
      bindGet,
      toggle,
      selectAll,
      confirm,
      cancel
    }
  },
})
</script>
<style lang="less" scoped>
.m-select {
  &-title {
    position: relative;
    text-align: center;
    line-height: 22px;
    font-size: 18px;
    font-weight: bold;
    padding: 8px 0;
    .m-title-block {
      display: inline-block;
      width: 100%;
    }
    .m-select-all {
      position: absolute;
      right: 16px;
      color: #666;
      ins {
        font-size: 14px;
        vertical-align: top;
        text-decoration: none;
      }
    }
  }
  &-content {
    overflow-y: auto;
    scrollbar-width: thin;
    -webkit-overflow-scrolling: touch;
  }
  .m-control-btn {
    display: flex;
    justify-content: center;
    // margin-top: 10px;
    padding: 12px 0;
    .m-btn {
      width: 92px;
      &.m-btn-confirm {
        margin-right: 6px;
      }
      &.m-btn-cancel {
        margin-left: 6px;
      }
    }
  }
}
</style>
